#include "common_config_init.h"
#include "common_media_define.h"
#include "common_product_definition.h"
#include "base_log.h"

namespace El {
namespace Common {

ConfigInit &ConfigInit::GetInstance()
{
    static ConfigInit instance;
    return instance;
}

ConfigInit::ConfigInit() {}

ConfigInit::~ConfigInit() {}

bool ConfigInit::InitDefaultConfig()
{
    InitLanguage();
    InitCloudModel();
    InitNetwork();
    InitStorage();
    InitRecord();
    InitAudio();
    InitOSD();
    return true;
}

void ConfigInit::InitLanguage()
{
    auto language = ConfigManager::GetInstance().GetConfig("language");
    if (language.is_null()) {
        std::vector<std::string> languages;
        if (ProductDefinition::GetInstance().GetValue("languages", languages)) {
            LOG_INFO("all languages: {}", fmt::join(languages, ", "));
            ConfigManager::GetInstance().SetConfig("language", languages.front());
        } else {
            ConfigManager::GetInstance().SetConfig("language", "zh_CN");
        }
    }
}

void ConfigInit::InitCloudModel()
{
    nlohmann::json config = Common::ConfigManager::GetInstance().GetDefaultConfig("cloud_model");
    if (config.is_null()) {
        config["api_key"] = "xxx";
        config["model_audio"] = "qwen2-audio-instruct";
        config["max_context_length_audio"] = 32000;
    }
    Common::ConfigManager::GetInstance().SetDefaultConfig("cloud_model", config);
}

void ConfigInit::InitNetwork()
{
    auto network = Common::ConfigManager::GetInstance().GetDefaultConfig("network");
    if (network.is_null()) {
        // 默认网卡配置
        network["defaultInterface"] = "eth0"; // 默认网卡名称

        // eth0网卡配置
        network["interfaces"]["eth0"]["enable"] = true;               // 是否启用
        network["interfaces"]["eth0"]["type"] = "ethernet";           // 网卡类型：ethernet
        network["interfaces"]["eth0"]["dhcp"] = true;                 // 是否启用DHCP
        network["interfaces"]["eth0"]["ipAddress"] = "192.168.1.100"; // 静态IP地址
        network["interfaces"]["eth0"]["netmask"] = "255.255.255.0";   // 子网掩码
        network["interfaces"]["eth0"]["gateway"] = "192.168.1.1";     // 网关地址
        network["interfaces"]["eth0"]["dns1"] = "8.8.8.8";            // 主DNS服务器
        network["interfaces"]["eth0"]["dns2"] = "8.8.4.4";            // 备用DNS服务器
        network["interfaces"]["eth0"]["metric"] = 100;                // 路由跃点数，数值越小优先级越高

        // 全局网络配置
        network["hostname"] = "ipc";         // 主机名
        network["primaryDNS"] = "8.8.8.8";   // 全局主DNS服务器
        network["secondaryDNS"] = "8.8.4.4"; // 全局备用DNS服务器
    }
    Common::ConfigManager::GetInstance().SetDefaultConfig("network", network);
}

void ConfigInit::InitRecord()
{
    auto record = Common::ConfigManager::GetInstance().GetDefaultConfig("record");
    if (record.is_null()) {
        record["enable"] = true;
        record["overwrite"] = true;          // 循环覆盖
        record["maxFileDuration"] = 30 * 60; // 录像文件最大时长，单位为秒
        record["maxFileSize"] = 2 * 1024;    // 录像文件最大大小，单位为MB
        record["fileFormat"] = "mp4";        // 录像文件格式
        record["preRecord"] = 10;            // 预录时长,单位秒
        record["delayRecord"] = 10;          // 延录时长,单位秒
        for (uint32_t i = 0; i < CHANNEL_MAX; i++) {
            record["record"][i]["enable"] = true; // 是否启用录像
            record["record"][i]["quality"] = 1;   // 录像质量等级:0-低,1-中,2-高
        }
    }
    Common::ConfigManager::GetInstance().SetDefaultConfig("record", record);
}

void ConfigInit::InitStorage()
{
    auto storage = Common::ConfigManager::GetInstance().GetDefaultConfig("storage");
    if (storage.is_null()) {
        storage["mountPath"] = "/mnt/udisk";
        storage["warningThreshold"] = 95; // 磁盘使用率超过95%时，发出警告
        storage["reserveSpace"] = 200;    // 预留磁盘空间,单位MB
    }
    Common::ConfigManager::GetInstance().SetDefaultConfig("storage", storage);
}

void ConfigInit::InitAudio()
{
    auto audio = Common::ConfigManager::GetInstance().GetDefaultConfig("audio");
    if (audio.is_null()) {
        audio["enable"] = false;
        audio["sampleRate"] = 16000;
        audio["bitRate"] = 16;
        audio["channel"] = 1;
    }
    Common::ConfigManager::GetInstance().SetDefaultConfig("audio", audio);
}

void ConfigInit::InitOSD()
{
    auto osd = Common::ConfigManager::GetInstance().GetDefaultConfig("osd");
    if (osd.is_null()) {
        osd["enable"] = true;
    }
    Common::ConfigManager::GetInstance().SetDefaultConfig("osd", osd);
}

} // namespace Common
} // namespace El
